package simple.cat.servlet;

import simple.cat.server.Request;
import simple.cat.server.Response;

/**
 * @author zheng9feng
 * @Description
 * @Date 2020/12/29 07:20
 */
public abstract class HttpServlet implements Servlet {
	/**
	 * 处理get请求
	 *
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	abstract void doGet(Request request, Response response) throws Exception;
	
	/**
	 * 处理post请求
	 *
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	abstract void doPost(Request request, Response response) throws Exception;
	
	/**
	 * 初始化方法
	 *
	 * @throws Exception
	 */
	@Override
	public void init() throws Exception {
	}
	
	/**
	 * 销毁
	 *
	 * @throws Exception
	 */
	@Override
	public void destroy() throws Exception {
	}
	
	/**
	 * 请求处理模板方法
	 *
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@Override
	public void service(Request request, Response response) throws Exception {
		String method = request.getMethod();
		if ("GET".equalsIgnoreCase(method)) {
			doGet(request, response);
		} else if ("POST".equalsIgnoreCase(method)) {
			doPost(request, response);
		}
	}
}
